import { AnyAction, Reducer } from 'redux';

import { EffectsCommandMap } from 'dva';

import { message } from 'antd';
import { router } from 'umi';
import { fetchValidation } from '@/utils';
import {
    queryOcrLogisDetail,
    opertionSetCount,
    opertionConfirm,
    opertionBehalfConfirm,
} from './service';

export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
    namespace: string;
    state: {};
    effects: {
        fetch: Effect;
        setCounting: Effect;
        confirm: Effect;
        behalfConfirm: Effect;
    };
    reducers: {
        show: Reducer;
        saveCheck: Reducer;
    };
}

const Model: ModelType = {
    namespace: 'ocrCheckDetail',

    state: {
        detailInfo: {},
        hasCheck: false,
    },

    effects: {
        * fetch({ payload }, { call, put }) {
            const response = yield call(queryOcrLogisDetail, payload);
            const data = fetchValidation(response);

            if (data) {
                if (data.msg === '成功') {
                    yield put({
                        type: 'show',
                        payload: response.body,
                    });
                } else {
                    router.push({ pathname: '/logisCenter/ocrCheck/logis' })
                }
            }
        },

        * setCounting({ payload }, { call, put }) {
            const response = yield call(opertionSetCount, payload);
            const data = fetchValidation(response);
            if (data) {
                message.success('操作成功');
                yield put({
                    type: 'fetch',
                    payload: { logisId: payload.logisId },
                });
            }
        },

        * confirm({ payload }, { call, put }) {
            const response = yield call(opertionConfirm, payload);
            const data = fetchValidation(response);
            if (data) {
                message.success('操作成功');
                yield put({
                    type: 'fetch',
                    payload: { logisId: payload.logisId, expressId: payload.expressId },
                });
            }
        },

        * behalfConfirm({ payload }, { call, put }) {
            const response = yield call(opertionBehalfConfirm, payload);
            const data = fetchValidation(response);
            if (data) {
                message.success('操作成功');
                yield put({
                    type: 'fetch',
                    payload: { logisId: payload.logisId, expressId: payload.expressId },
                });
            }
        },
    },

    reducers: {
        show(state, { payload }) {
            return {
                ...state,
                detailInfo: payload,
            };
        },

        saveCheck(state, { payload }) {
            return {
                ...state,
                hasCheck: payload,
            };
        },
    },
};

export default Model;
